package com.cos.lession73.slice;

import com.cos.lession73.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;

public class DouyinAbilitySlice extends AbilitySlice {
    //用于记录是否被点赞的标识符
    boolean like=false;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_douyin);
        DirectionalLayout layout = (DirectionalLayout) findComponentById(ResourceTable.Id_fu);
        // 获取图片组件对象
        Image img = (Image) findComponentById(ResourceTable.Id_like);
        //双击事件
        layout.setDoubleClickedListener(new Component.DoubleClickedListener() {
            @Override
            public void onDoubleClick(Component component) {
                //双击三联
                if (!like) {
                    //在为false即没有点赞过才能点赞
                    img.setImageAndDecodeBounds(ResourceTable.Media_like);
                    //设置状态
                    like=true;
                }
            }
        });
        //单击事件
        img.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (like) {
                    //已有点赞,就取消点赞
                    img.setImageAndDecodeBounds(ResourceTable.Media_unlike);
                    //设置状态
                    like=false;
                }else{
                    //没有点赞,设置点赞
                    img.setImageAndDecodeBounds(ResourceTable.Media_like);
                    like=true;
                }
            }
        });
    }
}
